package com.tmc.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.baseinfo.Continent;
import com.tmc.util.ObjectUtil;

@Repository
public class ContinentDaoImpl extends BaseDaoImpl<Continent> {

	public Pagination<Continent> findByCon(ContextInfo context, Continent cond, int pageIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Continent c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getContinentCode())) {
			sql.append(" and (c.continentCode like :continentCode or c.continentName like :continentCode or c.countryNames like :continentCode) ");
			params.put("continentCode", "%" + cond.getContinentCode() + "%");
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public List<Continent> findAll() {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Continent c where c.enabledFlag = 'Y' ");
		return this.findList(sql.toString(), params);
	}

	public Continent getByCode(ContextInfo context, String code) {
		if (!ObjectUtil.hasText(code)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Continent c where c.enabledFlag = 'Y' ");
		sql.append(" and c.continentCode = :code ");
		params.put("code", code);
		return this.get(sql.toString(), params);
	}
	
	public Continent getByName(ContextInfo context, String name) {
		if (!ObjectUtil.hasText(name)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Continent c where c.enabledFlag = 'Y' ");
		sql.append(" and c.continentName = :name ");
		params.put("name", name);
		return this.get(sql.toString(), params);
	}

}
